/*
  Copyright © 2010  José Cavalcante Reis Neto

      This file is part of Touché.

      Touché is free software: you can redistribute it and/or modify
      it under the terms of the GNU Lesser General Public License as published
      by the Free Software Foundation, either version 3 of the License, or
      (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU Lesser General Public License for more details.

      You should have received a copy of the GNU Lesser General Public License
      along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */

#include "photoelement.h"
#include <QGraphicsPixmapItem>
#include <QPushButton>
#include <QGridLayout>
#include <QGraphicsScene>
using namespace Touche;

// class PhotoWidget

PhotoWidget::PhotoWidget(Touche::Stage *stage) :
        stage(stage)
{
    this->setFixedSize(800, 480);
    //PIPELINE //
    {
        QPushButton *button = new QPushButton("Take Photo", this);
        button->setGeometry(672, 0, 128, 64);
        button->show();
        connect(button, SIGNAL(clicked()), this, SLOT(takePhoto()));

//        QLayout *layout = new QGridLayout;
//        layout->addWidget(this);
//        layout->addWidget(button);
    }

    pipeline = gst_pipeline_new("test-camera");

    cameraSrc   = gst_element_factory_make("v4l2src","Camera_src");

    filter       = gst_element_factory_make ("ffmpegcolorspace","filter");



    videoTee =  gst_element_factory_make("tee","videoTee");

    queue1 =  gst_element_factory_make("queue","queue1");

    screenSink = gst_element_factory_make("xvimagesink","screenSink");
    this->setAttribute(Qt::WA_NativeWindow, true);
    gst_x_overlay_set_xwindow_id (GST_X_OVERLAY(screenSink), this->winId());




    //Link os itens do pipe
    gst_bin_add_many(GST_BIN(pipeline),cameraSrc,filter,screenSink,videoTee,queue1,NULL);


    caps = gst_caps_new_simple("video/x-raw-yuv",
                               "width",     G_TYPE_INT,        640,
                               "height",    G_TYPE_INT,        480,
                               "framerate", GST_TYPE_FRACTION, 15,  1,
                               NULL);

    //link os elementos v4l2src , ffmpegcolorspace,video/x-raw-yuv
    gst_element_link_filtered(cameraSrc, filter, caps);
    g_print("problemas");

    gst_caps_unref(caps);

    gst_element_link_many(filter,videoTee,queue1,screenSink,NULL);
    g_print("foi");


    gst_element_link_many(cameraSrc,filter,NULL);
    g_print("foi");

    gst_element_set_state(pipeline,GST_STATE_PLAYING);

    //link os elementos ffmpegcolorspace, queue , xvimagesink
}

PhotoWidget::~PhotoWidget()
{
    gst_element_set_state(pipeline, GST_STATE_NULL);
    gst_object_unref(pipeline);
}

void PhotoWidget::takePhoto()
{
    PhotoElement *element = new PhotoElement;
    gst_element_set_state(pipeline,GST_STATE_READY);
    QString fileName("photo_" + element->id + ".jpg");

    pipelinephoto=gst_pipeline_new("photo pipeline");

    i = 0;

    photoSrc= gst_element_factory_make("v4l2src","photoSrc");

    photoFilter = gst_element_factory_make("ffmpegcolorspace","photoFilter");

    photoSink   = gst_element_factory_make("jpegenc","Photo_sink");

    sink         = gst_element_factory_make("filesink","sink");
    g_object_set(G_OBJECT(sink),
                 "location", fileName.toStdString().c_str(),
                 NULL);

    queue2 =  gst_element_factory_make("queue","queue2");

    gst_bin_add_many(GST_BIN(pipelinephoto),photoSrc,photoFilter,photoSink,sink,NULL);

    //    gst_element_link_filtered(cameraSrc, filter, caps);


    gst_element_link_many(photoSrc,photoFilter,photoSink,sink,NULL);


    g_print("palying");
    gst_element_set_state(pipelinephoto,GST_STATE_PLAYING);

    // TODO: diminuir este tempo, que está alto para o N900
    usleep(1350000);

    gst_element_set_state (pipelinephoto, GST_STATE_NULL);
    gst_object_unref (GST_OBJECT (pipelinephoto));
    g_print ("Deletando o pipeline\n");


    g_print("era pra estar gravando");
    gst_element_set_state(pipeline,GST_STATE_PLAYING);

    element->setFile(fileName);
    stage->addToGroup(element);

    gst_element_set_state(pipelinephoto, GST_STATE_NULL);
    gst_object_unref(pipelinephoto);
}

void PhotoWidget::showEvent(QShowEvent *)
{
    gst_x_overlay_expose(GST_X_OVERLAY(screenSink));
}

// class PhotoElement

static const QString type("photo");

PhotoElement::PhotoElement() :
        m_item(new QGraphicsPixmapItem)
{
    m_null = false;
    emit contentsChanged();
}

const QString &PhotoElement::type() const
{
    return ::type;
}

QRectF PhotoElement::item(QGraphicsItem *parentItem) const
{
    m_item->setParentItem(parentItem);
    return m_item->boundingRect();
}

QVector<QString> PhotoElement::menuOptions() const
{
    return QVector<QString>();
}

void PhotoElement::onMenuActived(const QString &)
{
    return;
}

void PhotoElement::setFile(const QString &file)
{
    this->file = file;
    m_item->setPixmap(QPixmap(file).scaled(120, 120, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
}
